/**
* 弹窗组件，使用： this.$store.dispatch('setModal', {visible: true, modalId: 'subscribeCancelModal',relationUserCode:'',callback:function(response){}});
*/
<template>
  <transition name="slide-fade">
    <div v-if="modal.visible && modal.modalId=='subscribeCancelModal'" class="modal" id="subscribeCancelModal" tabindex="-1" @click.stop.prevent="modalHide">
      <div class="modal-dialog" @click.stop.prevent>
        <div class="modal-content">
          <img src="~assets/images/tankuang-m@2x.png" class="img-responsive">
          <img src="~assets/images/close@2x.png" data-dismiss="modal" class="img-close" @click.stop.prevent="modalHide">
          <div class="oxhead">
            <img src="https://dl.app.gtja.com/nrng/shareimages/oxhead/niuwenhao.png">
          </div>
          <div class="wrapper">
            <h4>大王你确认要<span>取消订阅</span>TA么</h4>
            <p>小的听说取消订阅并不会还回君元宝哦~</p>
            <button type="button" class="btn btn-lg btn-agree" @click.stop.prevent="cancelSubscribe">退下！朕要取订</button>
            <button type="button" class="btn btn-lg btn-reject" @click.stop.prevent="modalHide">算了，留他观察一下</button>
          </div>
        </div>
      </div>
    </div>
  </transition>
</template>

<script>
  import BaseModal from 'components/modal/BaseModal'
  import RemoteDAO from 'common/remoteDAO'
  import LocalDAO from 'common/localDAO'
  import CommonUtil from 'common/common.util'
  import _ from 'underscore'

  export default {
    data () {
      return {}
    },
    extends:BaseModal,
    methods:{
      cancelSubscribe(){
        var self = this;
        var args = {};
        args.userCode = LocalDAO.getUserCode();
        args.actionType = "02";
        args.businType = "NRNG";
        args.communityType = "3";
        args.objectType = "1";
        args.relationUserCode = self.modal.relationUserCode;
        args.relationUserName = "";
        args.beginDate = "";
        args.endDate = "";
        args.communityContent = "";
        args.topicCode = "";
        args.topicName = "";
        args.serialNo = "";
        //取消订阅操作接口
        RemoteDAO.callUC12008306(args, function (response) {
          if(Number(response.error) === 0){
            var data = response.data.body || []
            if (data.length > 0 && data[0].error_no == "0") {
              self.$store.dispatch('setTip',{text:"取消订阅成功！"});
              LocalDAO.setMySubscribeCount(parseInt(LocalDAO.getMySubscribeCount()) - 1);
              var map = self.getSubMapFromSession();//订阅缓存
              map[args.relationUserCode]="2"; //取消订阅的再缓存里标记2
              sessionStorage.setItem('subscribeMap', JSON.stringify(map));

              if (_.isFunction(self.modal.callback)) {
                self.modal.callback(response);
              }
              self.modalHide();
            } else {
              self.$store.dispatch('setTip',{text:"取消订阅失败！"});
            }
          }else{
            self.$store.dispatch('setTip',{text:response.data});
          }
        });
      },
      getSubMapFromSession(){
        var map = sessionStorage.getItem('subscribeMap');
        if(typeof map === "undefined" || map == null) map = "{}";
        return JSON.parse(map);
      }
    }
  }
</script>

<style scoped>
  .slide-fade-enter-active,.slide-fade-leave-active{
    transition: all 0.3s ease;
  }
  .slide-fade-enter, .slide-fade-leave-active {
    transform: translate(0,-25%);
    opacity: 0;
  }
</style>
